import Cookies from 'js-cookie'
import { getTypes, queryUserList, queryAthletesList } from '@/api/setting'
const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  RATE_TITLE: [],
  VENUE: [],
  COURSE_TYPE: [],
  RESPORT_TYPE: [
    { 'typeCode': 'Master', 'typeName': 'Master Details', exports: 'master' },
    { 'typeCode': 'Allocation', 'typeName': 'Funding Allocation', exports: 'funding_allocation' },
    { 'typeCode': 'Sessions', 'typeName': 'Number of sessions', exports: 'number_of_session' },
    { 'typeCode': 'Autopay', 'typeName': 'Autopay months', exports: 'autopay' }],
    // week Day of the Week List
    WeekList: [
      { label: 'Mon', value: 'MONDAY' },
    { label: 'Tue', value: 'TUESDAY' },
    { label: 'Wed', value: 'WEDNESDAY' },
    { label: 'Thu', value: 'THURSDAY' },
    { label: 'Fri', value: 'FRIDAY' },
    { label: 'Sat', value: 'SATURDAY' },
    { label: 'Sun', value: 'SUNDAY' }
    ]

}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
    getType({ state }, code) {
    var getTypesFn = getTypes({ code })
    getTypesFn.then((suc) => {
    state[code] = suc.result
    }).catch(() => {
    state[code] = []
    })
    },
   async getAthletes({ state }, name) {
    var { result: data } = await queryAthletesList({ name })
    const suc = []
    for (var i in data) {
        suc.push({
            value: this._vm.FilterName(data[i], null, ' ', '(' + data[i].hkId + ')'),
            name: this._vm.FilterName(data[i], null, ' ', '(' + data[i].hkId + ')'),
            id: data[i].userId,
            hkId: data[i].hkId
        })
    }
     state['Athletes'] = suc
     return new Promise((solve) => {
     solve(suc)
    })
    },
    getNot_athletes({ state }, name) {
    var getTypesFn = queryUserList({ name })
    getTypesFn.then((suc) => {
    state['Not_athletes'] = suc.result
    }).catch(() => {
    state['Not_athletes'] = []
    })
    },
    // 全局导表 fn
    onExport({ state }, param) {
      var { barName, params } = param
      params = param.params
      var paramsStr = ''
      let ipr = 0
      for (const key of Object.keys(param.params)) {
        if (params[key]) {
            ipr++
          paramsStr += (ipr > 1 ? '&' : '') + key + '=' + (encodeURIComponent(params[key]))
        }
        }
      if(!params.token) return this._vm.$message({message:'Token does not exit!',type:'warning'})
     var apiUrl = process.env.VUE_APP_BASE_API
     console.log('export url',`${apiUrl}/export/${barName}/xls?${paramsStr}`,)
     window.open(`${apiUrl}/export/${barName}/xls?${paramsStr}`, 'blank')
    }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
